/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Ron
 */
public class FArticleJpaController implements Serializable {

    public FArticleJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    private EntityManagerFactory emf = null;

    public FArticleJpaController() {
        this.emf = Persistence.createEntityManagerFactory("projetT_RPU");
    }
    
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FArticle FArticle) throws PreexistingEntityException, Exception {
        if (FArticle.getFArticleCollection() == null) {
            FArticle.setFArticleCollection(new ArrayList<FArticle>());
        }
        if (FArticle.getFRessourceprodCollection() == null) {
            FArticle.setFRessourceprodCollection(new ArrayList<FRessourceprod>());
        }
        if (FArticle.getFDocligneCollection() == null) {
            FArticle.setFDocligneCollection(new ArrayList<FDocligne>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FArticle ARSubstitut = FArticle.getARSubstitut();
            if (ARSubstitut != null) {
                ARSubstitut = em.getReference(ARSubstitut.getClass(), ARSubstitut.getCbMarq());
                FArticle.setARSubstitut(ARSubstitut);
            }
            FCatalogue cbCLNo1 = FArticle.getCbCLNo1();
            if (cbCLNo1 != null) {
                cbCLNo1 = em.getReference(cbCLNo1.getClass(), cbCLNo1.getCbMarq());
                FArticle.setCbCLNo1(cbCLNo1);
            }
            FCatalogue cbCLNo2 = FArticle.getCbCLNo2();
            if (cbCLNo2 != null) {
                cbCLNo2 = em.getReference(cbCLNo2.getClass(), cbCLNo2.getCbMarq());
                FArticle.setCbCLNo2(cbCLNo2);
            }
            FCatalogue cbCLNo3 = FArticle.getCbCLNo3();
            if (cbCLNo3 != null) {
                cbCLNo3 = em.getReference(cbCLNo3.getClass(), cbCLNo3.getCbMarq());
                FArticle.setCbCLNo3(cbCLNo3);
            }
            FCatalogue cbCLNo4 = FArticle.getCbCLNo4();
            if (cbCLNo4 != null) {
                cbCLNo4 = em.getReference(cbCLNo4.getClass(), cbCLNo4.getCbMarq());
                FArticle.setCbCLNo4(cbCLNo4);
            }
            FRessourceprod RPCodeDefaut = FArticle.getRPCodeDefaut();
            if (RPCodeDefaut != null) {
                RPCodeDefaut = em.getReference(RPCodeDefaut.getClass(), RPCodeDefaut.getCbMarq());
                FArticle.setRPCodeDefaut(RPCodeDefaut);
            }
            Collection<FArticle> attachedFArticleCollection = new ArrayList<FArticle>();
            for (FArticle FArticleCollectionFArticleToAttach : FArticle.getFArticleCollection()) {
                FArticleCollectionFArticleToAttach = em.getReference(FArticleCollectionFArticleToAttach.getClass(), FArticleCollectionFArticleToAttach.getCbMarq());
                attachedFArticleCollection.add(FArticleCollectionFArticleToAttach);
            }
            FArticle.setFArticleCollection(attachedFArticleCollection);
            Collection<FRessourceprod> attachedFRessourceprodCollection = new ArrayList<FRessourceprod>();
            for (FRessourceprod FRessourceprodCollectionFRessourceprodToAttach : FArticle.getFRessourceprodCollection()) {
                FRessourceprodCollectionFRessourceprodToAttach = em.getReference(FRessourceprodCollectionFRessourceprodToAttach.getClass(), FRessourceprodCollectionFRessourceprodToAttach.getCbMarq());
                attachedFRessourceprodCollection.add(FRessourceprodCollectionFRessourceprodToAttach);
            }
            FArticle.setFRessourceprodCollection(attachedFRessourceprodCollection);
            Collection<FDocligne> attachedFDocligneCollection = new ArrayList<FDocligne>();
            for (FDocligne FDocligneCollectionFDocligneToAttach : FArticle.getFDocligneCollection()) {
                FDocligneCollectionFDocligneToAttach = em.getReference(FDocligneCollectionFDocligneToAttach.getClass(), FDocligneCollectionFDocligneToAttach.getCbMarq());
                attachedFDocligneCollection.add(FDocligneCollectionFDocligneToAttach);
            }
            FArticle.setFDocligneCollection(attachedFDocligneCollection);
            em.persist(FArticle);
            if (ARSubstitut != null) {
                ARSubstitut.getFArticleCollection().add(FArticle);
                ARSubstitut = em.merge(ARSubstitut);
            }
            if (cbCLNo1 != null) {
                cbCLNo1.getFArticleCollection().add(FArticle);
                cbCLNo1 = em.merge(cbCLNo1);
            }
            if (cbCLNo2 != null) {
                cbCLNo2.getFArticleCollection().add(FArticle);
                cbCLNo2 = em.merge(cbCLNo2);
            }
            if (cbCLNo3 != null) {
                cbCLNo3.getFArticleCollection().add(FArticle);
                cbCLNo3 = em.merge(cbCLNo3);
            }
            if (cbCLNo4 != null) {
                cbCLNo4.getFArticleCollection().add(FArticle);
                cbCLNo4 = em.merge(cbCLNo4);
            }
            if (RPCodeDefaut != null) {
                RPCodeDefaut.getFArticleCollection().add(FArticle);
                RPCodeDefaut = em.merge(RPCodeDefaut);
            }
            for (FArticle FArticleCollectionFArticle : FArticle.getFArticleCollection()) {
                FArticle oldARSubstitutOfFArticleCollectionFArticle = FArticleCollectionFArticle.getARSubstitut();
                FArticleCollectionFArticle.setARSubstitut(FArticle);
                FArticleCollectionFArticle = em.merge(FArticleCollectionFArticle);
                if (oldARSubstitutOfFArticleCollectionFArticle != null) {
                    oldARSubstitutOfFArticleCollectionFArticle.getFArticleCollection().remove(FArticleCollectionFArticle);
                    oldARSubstitutOfFArticleCollectionFArticle = em.merge(oldARSubstitutOfFArticleCollectionFArticle);
                }
            }
            for (FRessourceprod FRessourceprodCollectionFRessourceprod : FArticle.getFRessourceprodCollection()) {
                FArticle oldARRefDefautOfFRessourceprodCollectionFRessourceprod = FRessourceprodCollectionFRessourceprod.getARRefDefaut();
                FRessourceprodCollectionFRessourceprod.setARRefDefaut(FArticle);
                FRessourceprodCollectionFRessourceprod = em.merge(FRessourceprodCollectionFRessourceprod);
                if (oldARRefDefautOfFRessourceprodCollectionFRessourceprod != null) {
                    oldARRefDefautOfFRessourceprodCollectionFRessourceprod.getFRessourceprodCollection().remove(FRessourceprodCollectionFRessourceprod);
                    oldARRefDefautOfFRessourceprodCollectionFRessourceprod = em.merge(oldARRefDefautOfFRessourceprodCollectionFRessourceprod);
                }
            }
            for (FDocligne FDocligneCollectionFDocligne : FArticle.getFDocligneCollection()) {
                FArticle oldARRefOfFDocligneCollectionFDocligne = FDocligneCollectionFDocligne.getARRef();
                FDocligneCollectionFDocligne.setARRef(FArticle);
                FDocligneCollectionFDocligne = em.merge(FDocligneCollectionFDocligne);
                if (oldARRefOfFDocligneCollectionFDocligne != null) {
                    oldARRefOfFDocligneCollectionFDocligne.getFDocligneCollection().remove(FDocligneCollectionFDocligne);
                    oldARRefOfFDocligneCollectionFDocligne = em.merge(oldARRefOfFDocligneCollectionFDocligne);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findFArticle(FArticle.getCbMarq()) != null) {
                throw new PreexistingEntityException("FArticle " + FArticle + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FArticle FArticle) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FArticle persistentFArticle = em.find(FArticle.class, FArticle.getCbMarq());
            FArticle ARSubstitutOld = persistentFArticle.getARSubstitut();
            FArticle ARSubstitutNew = FArticle.getARSubstitut();
            FCatalogue cbCLNo1Old = persistentFArticle.getCbCLNo1();
            FCatalogue cbCLNo1New = FArticle.getCbCLNo1();
            FCatalogue cbCLNo2Old = persistentFArticle.getCbCLNo2();
            FCatalogue cbCLNo2New = FArticle.getCbCLNo2();
            FCatalogue cbCLNo3Old = persistentFArticle.getCbCLNo3();
            FCatalogue cbCLNo3New = FArticle.getCbCLNo3();
            FCatalogue cbCLNo4Old = persistentFArticle.getCbCLNo4();
            FCatalogue cbCLNo4New = FArticle.getCbCLNo4();
            FRessourceprod RPCodeDefautOld = persistentFArticle.getRPCodeDefaut();
            FRessourceprod RPCodeDefautNew = FArticle.getRPCodeDefaut();
            Collection<FArticle> FArticleCollectionOld = persistentFArticle.getFArticleCollection();
            Collection<FArticle> FArticleCollectionNew = FArticle.getFArticleCollection();
            Collection<FRessourceprod> FRessourceprodCollectionOld = persistentFArticle.getFRessourceprodCollection();
            Collection<FRessourceprod> FRessourceprodCollectionNew = FArticle.getFRessourceprodCollection();
            Collection<FDocligne> FDocligneCollectionOld = persistentFArticle.getFDocligneCollection();
            Collection<FDocligne> FDocligneCollectionNew = FArticle.getFDocligneCollection();
            if (ARSubstitutNew != null) {
                ARSubstitutNew = em.getReference(ARSubstitutNew.getClass(), ARSubstitutNew.getCbMarq());
                FArticle.setARSubstitut(ARSubstitutNew);
            }
            if (cbCLNo1New != null) {
                cbCLNo1New = em.getReference(cbCLNo1New.getClass(), cbCLNo1New.getCbMarq());
                FArticle.setCbCLNo1(cbCLNo1New);
            }
            if (cbCLNo2New != null) {
                cbCLNo2New = em.getReference(cbCLNo2New.getClass(), cbCLNo2New.getCbMarq());
                FArticle.setCbCLNo2(cbCLNo2New);
            }
            if (cbCLNo3New != null) {
                cbCLNo3New = em.getReference(cbCLNo3New.getClass(), cbCLNo3New.getCbMarq());
                FArticle.setCbCLNo3(cbCLNo3New);
            }
            if (cbCLNo4New != null) {
                cbCLNo4New = em.getReference(cbCLNo4New.getClass(), cbCLNo4New.getCbMarq());
                FArticle.setCbCLNo4(cbCLNo4New);
            }
            if (RPCodeDefautNew != null) {
                RPCodeDefautNew = em.getReference(RPCodeDefautNew.getClass(), RPCodeDefautNew.getCbMarq());
                FArticle.setRPCodeDefaut(RPCodeDefautNew);
            }
            Collection<FArticle> attachedFArticleCollectionNew = new ArrayList<FArticle>();
            for (FArticle FArticleCollectionNewFArticleToAttach : FArticleCollectionNew) {
                FArticleCollectionNewFArticleToAttach = em.getReference(FArticleCollectionNewFArticleToAttach.getClass(), FArticleCollectionNewFArticleToAttach.getCbMarq());
                attachedFArticleCollectionNew.add(FArticleCollectionNewFArticleToAttach);
            }
            FArticleCollectionNew = attachedFArticleCollectionNew;
            FArticle.setFArticleCollection(FArticleCollectionNew);
            Collection<FRessourceprod> attachedFRessourceprodCollectionNew = new ArrayList<FRessourceprod>();
            for (FRessourceprod FRessourceprodCollectionNewFRessourceprodToAttach : FRessourceprodCollectionNew) {
                FRessourceprodCollectionNewFRessourceprodToAttach = em.getReference(FRessourceprodCollectionNewFRessourceprodToAttach.getClass(), FRessourceprodCollectionNewFRessourceprodToAttach.getCbMarq());
                attachedFRessourceprodCollectionNew.add(FRessourceprodCollectionNewFRessourceprodToAttach);
            }
            FRessourceprodCollectionNew = attachedFRessourceprodCollectionNew;
            FArticle.setFRessourceprodCollection(FRessourceprodCollectionNew);
            Collection<FDocligne> attachedFDocligneCollectionNew = new ArrayList<FDocligne>();
            for (FDocligne FDocligneCollectionNewFDocligneToAttach : FDocligneCollectionNew) {
                FDocligneCollectionNewFDocligneToAttach = em.getReference(FDocligneCollectionNewFDocligneToAttach.getClass(), FDocligneCollectionNewFDocligneToAttach.getCbMarq());
                attachedFDocligneCollectionNew.add(FDocligneCollectionNewFDocligneToAttach);
            }
            FDocligneCollectionNew = attachedFDocligneCollectionNew;
            FArticle.setFDocligneCollection(FDocligneCollectionNew);
            FArticle = em.merge(FArticle);
            if (ARSubstitutOld != null && !ARSubstitutOld.equals(ARSubstitutNew)) {
                ARSubstitutOld.getFArticleCollection().remove(FArticle);
                ARSubstitutOld = em.merge(ARSubstitutOld);
            }
            if (ARSubstitutNew != null && !ARSubstitutNew.equals(ARSubstitutOld)) {
                ARSubstitutNew.getFArticleCollection().add(FArticle);
                ARSubstitutNew = em.merge(ARSubstitutNew);
            }
            if (cbCLNo1Old != null && !cbCLNo1Old.equals(cbCLNo1New)) {
                cbCLNo1Old.getFArticleCollection().remove(FArticle);
                cbCLNo1Old = em.merge(cbCLNo1Old);
            }
            if (cbCLNo1New != null && !cbCLNo1New.equals(cbCLNo1Old)) {
                cbCLNo1New.getFArticleCollection().add(FArticle);
                cbCLNo1New = em.merge(cbCLNo1New);
            }
            if (cbCLNo2Old != null && !cbCLNo2Old.equals(cbCLNo2New)) {
                cbCLNo2Old.getFArticleCollection().remove(FArticle);
                cbCLNo2Old = em.merge(cbCLNo2Old);
            }
            if (cbCLNo2New != null && !cbCLNo2New.equals(cbCLNo2Old)) {
                cbCLNo2New.getFArticleCollection().add(FArticle);
                cbCLNo2New = em.merge(cbCLNo2New);
            }
            if (cbCLNo3Old != null && !cbCLNo3Old.equals(cbCLNo3New)) {
                cbCLNo3Old.getFArticleCollection().remove(FArticle);
                cbCLNo3Old = em.merge(cbCLNo3Old);
            }
            if (cbCLNo3New != null && !cbCLNo3New.equals(cbCLNo3Old)) {
                cbCLNo3New.getFArticleCollection().add(FArticle);
                cbCLNo3New = em.merge(cbCLNo3New);
            }
            if (cbCLNo4Old != null && !cbCLNo4Old.equals(cbCLNo4New)) {
                cbCLNo4Old.getFArticleCollection().remove(FArticle);
                cbCLNo4Old = em.merge(cbCLNo4Old);
            }
            if (cbCLNo4New != null && !cbCLNo4New.equals(cbCLNo4Old)) {
                cbCLNo4New.getFArticleCollection().add(FArticle);
                cbCLNo4New = em.merge(cbCLNo4New);
            }
            if (RPCodeDefautOld != null && !RPCodeDefautOld.equals(RPCodeDefautNew)) {
                RPCodeDefautOld.getFArticleCollection().remove(FArticle);
                RPCodeDefautOld = em.merge(RPCodeDefautOld);
            }
            if (RPCodeDefautNew != null && !RPCodeDefautNew.equals(RPCodeDefautOld)) {
                RPCodeDefautNew.getFArticleCollection().add(FArticle);
                RPCodeDefautNew = em.merge(RPCodeDefautNew);
            }
            for (FArticle FArticleCollectionOldFArticle : FArticleCollectionOld) {
                if (!FArticleCollectionNew.contains(FArticleCollectionOldFArticle)) {
                    FArticleCollectionOldFArticle.setARSubstitut(null);
                    FArticleCollectionOldFArticle = em.merge(FArticleCollectionOldFArticle);
                }
            }
            for (FArticle FArticleCollectionNewFArticle : FArticleCollectionNew) {
                if (!FArticleCollectionOld.contains(FArticleCollectionNewFArticle)) {
                    FArticle oldARSubstitutOfFArticleCollectionNewFArticle = FArticleCollectionNewFArticle.getARSubstitut();
                    FArticleCollectionNewFArticle.setARSubstitut(FArticle);
                    FArticleCollectionNewFArticle = em.merge(FArticleCollectionNewFArticle);
                    if (oldARSubstitutOfFArticleCollectionNewFArticle != null && !oldARSubstitutOfFArticleCollectionNewFArticle.equals(FArticle)) {
                        oldARSubstitutOfFArticleCollectionNewFArticle.getFArticleCollection().remove(FArticleCollectionNewFArticle);
                        oldARSubstitutOfFArticleCollectionNewFArticle = em.merge(oldARSubstitutOfFArticleCollectionNewFArticle);
                    }
                }
            }
            for (FRessourceprod FRessourceprodCollectionOldFRessourceprod : FRessourceprodCollectionOld) {
                if (!FRessourceprodCollectionNew.contains(FRessourceprodCollectionOldFRessourceprod)) {
                    FRessourceprodCollectionOldFRessourceprod.setARRefDefaut(null);
                    FRessourceprodCollectionOldFRessourceprod = em.merge(FRessourceprodCollectionOldFRessourceprod);
                }
            }
            for (FRessourceprod FRessourceprodCollectionNewFRessourceprod : FRessourceprodCollectionNew) {
                if (!FRessourceprodCollectionOld.contains(FRessourceprodCollectionNewFRessourceprod)) {
                    FArticle oldARRefDefautOfFRessourceprodCollectionNewFRessourceprod = FRessourceprodCollectionNewFRessourceprod.getARRefDefaut();
                    FRessourceprodCollectionNewFRessourceprod.setARRefDefaut(FArticle);
                    FRessourceprodCollectionNewFRessourceprod = em.merge(FRessourceprodCollectionNewFRessourceprod);
                    if (oldARRefDefautOfFRessourceprodCollectionNewFRessourceprod != null && !oldARRefDefautOfFRessourceprodCollectionNewFRessourceprod.equals(FArticle)) {
                        oldARRefDefautOfFRessourceprodCollectionNewFRessourceprod.getFRessourceprodCollection().remove(FRessourceprodCollectionNewFRessourceprod);
                        oldARRefDefautOfFRessourceprodCollectionNewFRessourceprod = em.merge(oldARRefDefautOfFRessourceprodCollectionNewFRessourceprod);
                    }
                }
            }
            for (FDocligne FDocligneCollectionOldFDocligne : FDocligneCollectionOld) {
                if (!FDocligneCollectionNew.contains(FDocligneCollectionOldFDocligne)) {
                    FDocligneCollectionOldFDocligne.setARRef(null);
                    FDocligneCollectionOldFDocligne = em.merge(FDocligneCollectionOldFDocligne);
                }
            }
            for (FDocligne FDocligneCollectionNewFDocligne : FDocligneCollectionNew) {
                if (!FDocligneCollectionOld.contains(FDocligneCollectionNewFDocligne)) {
                    FArticle oldARRefOfFDocligneCollectionNewFDocligne = FDocligneCollectionNewFDocligne.getARRef();
                    FDocligneCollectionNewFDocligne.setARRef(FArticle);
                    FDocligneCollectionNewFDocligne = em.merge(FDocligneCollectionNewFDocligne);
                    if (oldARRefOfFDocligneCollectionNewFDocligne != null && !oldARRefOfFDocligneCollectionNewFDocligne.equals(FArticle)) {
                        oldARRefOfFDocligneCollectionNewFDocligne.getFDocligneCollection().remove(FDocligneCollectionNewFDocligne);
                        oldARRefOfFDocligneCollectionNewFDocligne = em.merge(oldARRefOfFDocligneCollectionNewFDocligne);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = FArticle.getCbMarq();
                if (findFArticle(id) == null) {
                    throw new NonexistentEntityException("The fArticle with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FArticle FArticle;
            try {
                FArticle = em.getReference(FArticle.class, id);
                FArticle.getCbMarq();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The FArticle with id " + id + " no longer exists.", enfe);
            }
            FArticle ARSubstitut = FArticle.getARSubstitut();
            if (ARSubstitut != null) {
                ARSubstitut.getFArticleCollection().remove(FArticle);
                ARSubstitut = em.merge(ARSubstitut);
            }
            FCatalogue cbCLNo1 = FArticle.getCbCLNo1();
            if (cbCLNo1 != null) {
                cbCLNo1.getFArticleCollection().remove(FArticle);
                cbCLNo1 = em.merge(cbCLNo1);
            }
            FCatalogue cbCLNo2 = FArticle.getCbCLNo2();
            if (cbCLNo2 != null) {
                cbCLNo2.getFArticleCollection().remove(FArticle);
                cbCLNo2 = em.merge(cbCLNo2);
            }
            FCatalogue cbCLNo3 = FArticle.getCbCLNo3();
            if (cbCLNo3 != null) {
                cbCLNo3.getFArticleCollection().remove(FArticle);
                cbCLNo3 = em.merge(cbCLNo3);
            }
            FCatalogue cbCLNo4 = FArticle.getCbCLNo4();
            if (cbCLNo4 != null) {
                cbCLNo4.getFArticleCollection().remove(FArticle);
                cbCLNo4 = em.merge(cbCLNo4);
            }
            FRessourceprod RPCodeDefaut = FArticle.getRPCodeDefaut();
            if (RPCodeDefaut != null) {
                RPCodeDefaut.getFArticleCollection().remove(FArticle);
                RPCodeDefaut = em.merge(RPCodeDefaut);
            }
            Collection<FArticle> FArticleCollection = FArticle.getFArticleCollection();
            for (FArticle FArticleCollectionFArticle : FArticleCollection) {
                FArticleCollectionFArticle.setARSubstitut(null);
                FArticleCollectionFArticle = em.merge(FArticleCollectionFArticle);
            }
            Collection<FRessourceprod> FRessourceprodCollection = FArticle.getFRessourceprodCollection();
            for (FRessourceprod FRessourceprodCollectionFRessourceprod : FRessourceprodCollection) {
                FRessourceprodCollectionFRessourceprod.setARRefDefaut(null);
                FRessourceprodCollectionFRessourceprod = em.merge(FRessourceprodCollectionFRessourceprod);
            }
            Collection<FDocligne> FDocligneCollection = FArticle.getFDocligneCollection();
            for (FDocligne FDocligneCollectionFDocligne : FDocligneCollection) {
                FDocligneCollectionFDocligne.setARRef(null);
                FDocligneCollectionFDocligne = em.merge(FDocligneCollectionFDocligne);
            }
            em.remove(FArticle);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FArticle> findFArticleEntities() {
        return findFArticleEntities(true, -1, -1);
    }

    public List<FArticle> findFArticleEntities(int maxResults, int firstResult) {
        return findFArticleEntities(false, maxResults, firstResult);
    }

    private List<FArticle> findFArticleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(FArticle.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FArticle findFArticle(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FArticle.class, id);
        } finally {
            em.close();
        }
    }

    public int getFArticleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<FArticle> rt = cq.from(FArticle.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
